package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ServiceProviderCreditDTO;
import com.eastfair.venueservice.dto.ServiceProviderCreditPageQuery;
import com.eastfair.venueservice.dto.ServiceProviderCreditSaveDTO;
import com.eastfair.venueservice.dto.ServiceProviderCreditUpdateDTO;
import com.eastfair.venueservice.entity.ServiceProviderCredit;
import com.eastfair.venueservice.service.ServiceProviderCreditService;
import com.eastfair.venueservice.vo.ServiceProviderCreditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务商信用表
 * </p>
 *
 * @author lyw
 * @date 2022 -08-11
 */
@Deprecated
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceProviderCredit")
@Api(value = "ServiceProviderCredit", tags = "服务商信用表")
@PreAuth(replace = "common:serviceProviderCredit:")
public class ServiceProviderCreditController extends SuperController<ServiceProviderCreditService, Long, ServiceProviderCredit, ServiceProviderCreditPageQuery, ServiceProviderCreditSaveDTO, ServiceProviderCreditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ServiceProviderCreditService serviceProviderCreditService;

    @Override
    public void handlerResult(IPage<ServiceProviderCredit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceProviderCredit> serviceProviderCreditList = list
                .stream()
                .map((map) -> ServiceProviderCredit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(serviceProviderCreditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceProviderCreditPageQuery> params) {
        ServiceProviderCreditPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 添加服务商信用星级分
     *
     * @param serviceProviderCreditDTO the service provider credit dto
     * @return the r
     */
    @ApiOperation(value = "添加服务商信用星级分")
    @PostMapping(value = "/saveServiceProviderCreditInfo")
    public R<Boolean> saveServiceProviderCreditInfo(@RequestBody ServiceProviderCreditDTO serviceProviderCreditDTO){
        try {
            return R.success(serviceProviderCreditService.saveServiceProviderCredit(serviceProviderCreditDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 修改服务商信用星级分
     *
     * @param serviceProviderCreditDTO the service provider credit dto
     * @return the r
     */
    @ApiOperation(value = "修改服务商信用星级分")
    @PostMapping(value = "/updateServiceProviderCreditInfo")
    public R<Boolean> updateServiceProviderCreditInfo(@RequestBody ServiceProviderCreditDTO serviceProviderCreditDTO){
        try {
            return R.success(serviceProviderCreditService.updateServiceProviderCredit(serviceProviderCreditDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询服务商星级分
     *
     * @param serviceProviderId the service provider id
     * @return r
     */
    @ApiOperation(value = "查询服务商星级分")
    @GetMapping(value = "/getServiceProviderCreditById/{serviceProviderId}")
    public R<ServiceProviderCreditVO> getServiceProviderCreditById(@PathVariable Long serviceProviderId){
        try {
            return R.success(serviceProviderCreditService.getServiceProviderCreditVOById(serviceProviderId));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

}
